package com.sinoglobal.wallet.activity;

import com.sinoglobal.wallet.R;
import com.sinoglobal.wallet.dialog.LoadingDialog1;
import com.sinoglobal.wallet.util.LogUtils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

/**
 * 
 * @author jthou
 * @createdate 2016-1-7 下午4:40:29
 * @Description: 处理外部链接
 */
public class W_ActionActivity extends W_SinoBaseActivity {
private WebView webView;
private String message = "加载中...";//提示信息
private Myhandler handler;
private W_ActionActivity context;
private ProgressBar progressBar;
      public class   Myhandler extends Handler{

		@Override
		public void handleMessage(Message msg) {
                switch(msg.what){
                case 1:
                	 progressBar.setVisibility(View.GONE);
    				break;
                }
		
		
		}
    	   
      }
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wallet_activity_action);
		 webView = (WebView) findViewById(R.id.webView);
		  progressBar = (ProgressBar) findViewById(R.id.wallet_action_rules_pro);
		String url = getIntent().getStringExtra("url");
		String THIRD_PARTY_NAME = getIntent().getStringExtra("THIRD_PARTY_NAME");
		  if(THIRD_PARTY_NAME!=null){
      		mTemplateTitleText.setText(THIRD_PARTY_NAME);
         }
		LogUtils.v("url========="+url);
		  handler=new Myhandler();
			webView.loadUrl(url);
	
		  WebSettings webSettings = webView.getSettings();  
		 //设置WebView属性，能够执行Javascript脚本    
        webSettings.setJavaScriptEnabled(true);    
        //设置可以访问文件  
        webSettings.setAllowFileAccess(true);  
         //设置支持缩放  
        webSettings.setBuiltInZoomControls(true);  
        //加载需要显示的网页    
		webView.setWebViewClient(new WebViewClient(){});
		MyWebViewClient myWebView = new MyWebViewClient();
		webView.setWebViewClient(myWebView);
		 webView.setDownloadListener(new MyWebViewDownLoadListener());
		 webView.setWebChromeClient(new WebChromeClient());
		
	}
	private class MyWebViewDownLoadListener implements DownloadListener {  
		  
        @Override  
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,  
                                    long contentLength) {  
            Uri uri = Uri.parse(url);  
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);  
            startActivity(intent);  
        }  
  
    }  
	   public class WebChromeClient extends android.webkit.WebChromeClient {
	        @Override
	        public void onProgressChanged(WebView view, int newProgress) {
	            if (newProgress == 100) {
	                progressBar.setVisibility(View.GONE);
	            } else {
	                if (progressBar.getVisibility() == View.GONE)
	                    progressBar.setVisibility(View.VISIBLE);
	                progressBar.setProgress(newProgress);
	            }
	            super.onProgressChanged(view, newProgress);
	        }

	    }
	private class MyWebViewClient extends WebViewClient {

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}

		@Override
		public void onPageFinished(WebView view, String url) {
		}

		@Override
		public void onReceivedSslError(WebView view,
				android.webkit.SslErrorHandler handler,
				android.net.http.SslError error) {
			handler.proceed();
		};

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			  progressBar.setVisibility(View.VISIBLE);
			handler.sendEmptyMessageDelayed(1, 4000);//避免加载数据过多提示框不消失。5秒后自动消失
			
		};

		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
		   showToast("数据加载失败，请重试");
		 progressBar.setVisibility(View.GONE);
			super.onReceivedError(view, errorCode, description, failingUrl);

		}
	}

	  @Override   
	    //设置回退    
	    //覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法    
	    public boolean onKeyDown(int keyCode, KeyEvent event) {    
	        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { 
	        	webView.goBack(); //goBack()表示返回WebView的上一页面    
	            return true;    
	        }   
	        finish();
	        return false;    
	    } 
	  @Override
		protected void onDestroy() {
			// TODO Auto-generated method stub
			super.onDestroy();
			if (handler != null) {
				handler.removeCallbacksAndMessages(null); //当界面销毁时清除handler队列信息
			}
		}
	  
	 
}
